<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace longbingcore\wxcore;

class WxTmpl
{
    protected static $uniacid;
    public function __construct($uniacid)
    {
        self::$uniacid = $uniacid;
    }
    public static function addtmpl($dis)
    {
        $tmpl = \think\facade\Db::name("longbing_card_tmpl_config")->where($dis)->find();
        if(empty($tmpl["tmpl_id"])) {
            $service_model = new WxSetting(self::$uniacid);
            $send_data = ["tid" => $tmpl["tid"], "kidList" => explode(",", $tmpl["kid"]), "sceneDesc" => $tmpl["sceneDesc"]];
            $tmpl_info = $service_model->addTmpl($send_data);
            $tmpl_info = json_decode($tmpl_info, true);
        } else {
            $tmpl_info["priTmplId"] = $tmpl["tmpl_id"];
            $tmpl_info["errcode"] = 0;
        }
        return $tmpl_info;
    }
    public static function getTmplKey($tmpl_id)
    {
        if(empty($tmpl_id)) {
            return [];
        }
        $cach_data = getCache($tmpl_id, self::$uniacid);
        if(!empty($cach_data)) {
            return $cach_data;
        }
        $service_model = new WxSetting(self::$uniacid);
        $data = $service_model->getUserTmpl();
        if(empty($data)) {
            return [];
        }
        $data = json_decode($data, true);
        if(empty($data["data"])) {
            return [];
        }
        $data = $data["data"];
        $found_key = array_search($tmpl_id, array_column($data, "priTmplId"));
        if(!is_numeric($found_key)) {
            return [];
        }
        $tmpl = $data[$found_key];
        $content = explode("{{", $tmpl["content"]);
        foreach ($content as $k => $value) {
            if($k == 0) {
                unset($content[$k]);
            } else {
                $content[$k] = substr($value, 0, strpos($value, "."));
            }
        }
        setCache($tmpl_id, $content, 86400, self::$uniacid);
        return $content;
    }
    public static function tmplParam($key)
    {
        $data["pay_order"] = ["tid" => 1754, "kidList" => "thing4,amount2,character_string1,date3", "sceneDesc" => "订单支付成功", "kid" => "4,2,1,3", "example" => "物品名称、金额、单号、支付时间"];
        $data["send_order"] = ["tid" => 2414, "kidList" => "", "sceneDesc" => "订单发货通知", "kid" => "1,2,3,4", "example" => "发货时间、订单编号、物流编号、物流公司"];
        $data["im_msg"] = ["tid" => 1076, "kidList" => "", "sceneDesc" => "未读私信通知", "kid" => "1,2,3", "example" => "留言人、留言内容、留言时间"];
        if(key_exists($key, $data)) {
            return $data[$key];
        }
        return [];
    }
    public static function sendTmpl($poenid, $tmpl_id, $send_data, $page)
    {
        $data = ["touser" => $poenid, "template_id" => $tmpl_id, "page" => $page, "data" => $send_data];
        $service_model = new WxSetting(self::$uniacid);
        $res = $service_model->sendTmpl($data);
        return $res;
    }
    public static function strToUtf8($str)
    {
        $encode = mb_detect_encoding($str, ["ASCII", "UTF-8", "GB2312", "GBK", "BIG5"]);
        if($encode == "UTF-8") {
            return $str;
        }
        return mb_convert_encoding($str, "UTF-8", $encode);
    }
}

?>